import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.sql.* ;

public class loginPage extends MainFrame
{
  /* Things on the Log in page */
  JLabel title;
  JLabel passwordText;
  JPasswordField passwordLogin;
  JButton btnLogin;
  
  /* Animation Player */
  JLabel image;
  JLabel image2;
  private float alpha = 0.0F;  
  private float increment = 0.02F;  
  private boolean reverse = false;
  Timer animationPlayer;
  int numberAnimation = 1;
  
  
   public void Draw(){
   /* Password Text */
   passwordText = new JLabel("Password");
   passwordText.setFont(new Font("Cambria", Font.PLAIN, 18));
   passwordText.setForeground(Color.white);
   passwordText.setBounds(480,-65,200,200);
   MainFrame.panel.add(passwordText);
   
   
    title = new JLabel("Aplasca - Liganor Dental Clinic ");
    title.setFont(new Font("Cambria", Font.PLAIN, 17));
    title.setForeground(Color.black);
    title.setBounds(250,420,370,50);
    MainFrame.panel.add(title);
    
    title = new JLabel("Patient Achive and Appointments Manager ");
    title.setFont(new Font("Cambria", Font.PLAIN, 17));
    title.setForeground(Color.black);
    title.setBounds(210,440,370,50);
    MainFrame.panel.add(title);
    
    title = new JLabel("(+6302)913-9581 / (+63)927-245-6345");
    title.setFont(new Font("Cambria", Font.PLAIN, 17));
    title.setForeground(Color.black);
    title.setBounds(220,460,370,50);
    MainFrame.panel.add(title);
    
    title = new JLabel("mildred.liganor@yahoo.com.ph");
    title.setFont(new Font("Cambria", Font.PLAIN, 17));
    title.setForeground(Color.black);
    title.setBounds(250,480,370,50);
    MainFrame.panel.add(title);
        
   /* Password Log-in Area */
   passwordLogin = new JPasswordField(10);
   passwordLogin.setBounds(480,50,210,30);
   passwordLogin.setFont(new java.awt.Font("Cambria",java.awt.Font.PLAIN,16));
   MainFrame.panel.add(passwordLogin);
        
   /* Log in Button */
   btnLogin = new JButton(new ImageIcon("loginButton.jpg"));
   btnLogin.setBounds(700,50,84,30);
   btnLogin.addActionListener(new ButtonListener());
   MainFrame.panel.add(btnLogin);
   
   image2 = new JLabel(new ImageIcon("sides.png"));  
   image2.setBounds(0,0,800,600);
   MainFrame.panel.add(image2);
   
   alpha = 1.0F; numberAnimation = 1;
   image = new JLabel(new ImageIcon("dental1.jpg"));  
   image.setBounds(0,207,800,163);
   image.setForeground(Color.WHITE);
   MainFrame.panel.add(image);
   

   
    animationPlayer = new Timer(80, new ActionListener() {  
  
      public void actionPerformed(ActionEvent e) {  
          if(alpha < 0.99F && reverse == false)
            alpha += increment;
          else if (reverse == false){
            alpha = 0.99F;
            reverse = true;
          }
            
          if(alpha > 0.02F && reverse == true)
            alpha -= increment;
          else if (reverse == true){
            alpha = 0.02F;
            reverse = false;
            if(numberAnimation!=2)
                numberAnimation++;
            else
                numberAnimation = 1;
          }
          switch(numberAnimation){
              case 1:
                image.setIcon(new AlphaImageIcon(new ImageIcon("dental1.jpg"), alpha));  
                break;
              case 2:
                image.setIcon(new AlphaImageIcon(new ImageIcon("dental2.jpg"), alpha));  
                break;
            }
      }  
    });
    animationPlayer.start(); 
    
      
  }
  
   public class ButtonListener implements ActionListener{

     public void actionPerformed(ActionEvent e){      
       if(e.getSource() == btnLogin){
           try{
                        database.stmt = database.conn.createStatement();

                        database.rs = database.stmt.executeQuery("SELECT * FROM ACCOUNT");

                        while( database.rs.next() ){
                           if(database.rs.getString("password").equals(passwordLogin.getText())){
                             MainFrame.panel.removeAll();
                             MainFrame.homePage.Draw();
                             MainFrame.header.Draw();
                             MainFrame.panel.add(MainFrame.backgroundImage);
                             MainFrame.panel.repaint();
                             animationPlayer.stop();
                            }
                            else{
                            JOptionPane.showMessageDialog(null,"Wrong password. Please try again.");
                            }
                        }
                        
                           
                } catch( SQLException se ){}
        }
     }
   }
  
}
